<template>
	<view>
		<info :num="num" :money="money"></info>
		<tabs :list="list" :active="active" @tabBtn="tabBtn"></tabs>
		<view class="p-b-120">
			<list :orderList="orderList" @orderOk="orderOk" @seeListDetail="seeListDetail"></list>
		</view>
		<tabBar :type2="false"></tabBar>
		<u-modal v-model="show" :show-cancel-button="true" :confirm-text="'确认价格'" @confirm="surePrice">
			<view class="p-25">
				<input type="number" placeholder="请点击输入价格" class="borderB" v-model="price">
			</view>
		</u-modal>
	</view>
</template>

<script>
	import info from './components/info.vue'
	import tabs from './components/tabs.vue'
	import list from './components/list.vue'
	export default{
		components:{
			info,tabs,list
		},
		data(){
			return{
				price:"",
				active:0,
				list:['待确认','已确认','已完成','已取消'],
				page:1,
				orderList:[],
				sureId:"",
				num:0,
				money:0,
				show:false,
				Interval:',',
			}
		},
	
		onLoad(){
			this.getPrice()
			this.getInfo(this.page,this.active)  
				
			 var that = this
	  this.Interval =  setInterval( function() {
				that.getInfo(that.page,that.active)
			},3000) 
	
		},
		
		destroyed(){
			clearInterval(this.Interval)
		},
		methods:{
			async getPrice(){
				let data = await this.$request.post('/api/common/userinfo')
				console.log(data)
				let result = data.data.data
				this.money = result.total_money
				this.num = result.total_push_num
			},
			async getInfo(page,status){
				let data = await this.$request.post('/api/consumer/receivingOrderList',{
					page:page,
					status:status,
				})
				console.log(data,'司机我的订单')
				this.orderList = data.data.data 
			},
			seeListDetail(id){
				uni.navigateTo({
					url:"/pages/orderDetail/index?id=" + id + "&type=" + 1
				})
			},
			async orderOk(id){
				let data = await this.$request.post('/api/Consumer/is_evaluation',{oid:id})
				this.sureId = id
				if(data.data.code === 2){
					uni.showToast({
						title:"已提交最终价格，请等待车主方确认",
						icon:"none",
						duration:2000
					})
					return
				}else if(data.data.code === 3){
					uni.showToast({
						title:"请先评价车主，然后提交订单价格",
						icon:"none",
						duration:2000
					})
					setTimeout(()=>{
						uni.navigateTo({
							url:"/pages/carOwner/evaDrive/index?id=" + id + "&type=" + 1
						})
					},1500)
					return
				}else{
					uni.showToast({
						title:"请与车主协商最终价格",
						icon:"none",
						duration:2000
					})
					setTimeout(()=>{
						this.show = true
					},2000)
				}
			},
			async surePrice(){
				if(this.price != this.money){
					uni.showToast({
						title:"发单价格和最终价格不一致",
						icon:"none",
						duration:3000
					})
				}
				let data = await this.$request.post('/api/consumer/doneOrder',{
					oid:this.sureId,
					money:this.price,
				})
				uni.showToast({
					title:data.data.msg,
					icon:"none",
					duration:3000
				})
				console.log(data)
				if(data.data.code == 1){
					this.getInfo(this.page,this.active)
					let data = JSON.stringify({type:'sendorder',oid:this.sureId,token:uni.getStorageSync("token")})
					uni.sendSocketMessage({
						data:data,
						header: {
							'content-type': 'application/json'
						},
						success(res) {
							console.log(res)
						},
						fail(err) {
							console.log(err)
						}
					})
				}
			},
			tabBtn(key){
				this.active = key
				console.log(key)
				this.getInfo(this.page,key)
			},
		}
	}
</script>

<style lang="scss">
	.driver-info{
		position:absolute;
		top:0;
		left:0;
	}
</style>
